﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GatewayInspect.Common;
using GatewayInspect.Modules;
using GatewayInspect.Model;

namespace GatewayInspect
{
    public partial class MainForm : Form
    {
        private bool bTreeNodeAutoSelect = false;
        private bool bTabPageAutoSelect = false;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            SetStatus();//设置状态栏
            ApplyUserRight();//权限
        }

        private void SetStatus()
        {
            string strName = Session.CurrentOrgName;
            if (Session.CurrentOrgType == (int)OrgType.KQ)
            {
                strName += "(库区)";
            }
            else if (Session.CurrentOrgType == (int)OrgType.TCC)
            {
                strName += "(停车场)";
            }
            this.statusLblOrg.Text = "库区：" + strName;
            this.statusLblUser.Text = "当前用户：" + Session.CurrentUserXM;
            this.statusLblRole.Text = "角色：" + (Session.CurrentUserRole == "1" ? "管理员" : "普通用户");
        }

        private void ApplyUserRight()
        {
            if (Session.CurrentUserRole == "1" && Session.CurrentUserName == "ADMIN")
            {

                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeInOutGateJL");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeDJKInOutGateJL");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeASInOutGateJL");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodePAInOutGateJL");


            }
            else if (Session.CurrentUserName == "ADMIN_AS")
            {
                tvModuleList.Nodes.RemoveByKey("NodeSysSetting");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeInOutGateJL");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodePAInOutGateJL");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeALLInOutGateJL");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeDJKInOutGateJL");
                tvModuleList.Nodes["NodesMg"].Nodes.RemoveByKey("NodeFK");
                tvModuleList.Nodes["NodesMg"].Nodes.RemoveByKey("NodeFKJL");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeFczlDJK");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeFCZLSXDJK");
                //tvModuleList.Nodes["NodeSysSetting"].Nodes.RemoveByKey("NodeKQGL");
                
            }
            else if (Session.CurrentUserName == "ADMIN_PA")
            {
                tvModuleList.Nodes.RemoveByKey("NodeSysSetting");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeInOutGateJL");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeASInOutGateJL");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeALLInOutGateJL");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeDJKInOutGateJL");
                tvModuleList.Nodes["NodesMg"].Nodes.RemoveByKey("NodeFK");
                tvModuleList.Nodes["NodesMg"].Nodes.RemoveByKey("NodeFKJL");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeFczlDJK");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeFCZLSXDJK");
                //tvModuleList.Nodes["NodeSysSetting"].Nodes.RemoveByKey("NodeKQGL");

            }
            else if (Session.CurrentUserName == "ADMIN_DJK")
            {
                tvModuleList.Nodes.RemoveByKey("NodeSysSetting");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeInOutGateJL");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeASInOutGateJL");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodePAInOutGateJL");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeALLInOutGateJL");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeFczl");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeFCZLSX");
                tvModuleList.Nodes["NodesMg"].Nodes.RemoveByKey("NodeFK");
                tvModuleList.Nodes["NodesMg"].Nodes.RemoveByKey("NodeFKJL");
                //tvModuleList.Nodes["NodeSysSetting"].Nodes.RemoveByKey("NodeKQGL");
                //tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeDJKInOutGateJL");

            }
            else if (Session.CurrentUserName == "DJK" || Session.CurrentUserName == "MKR")
            {
                tvModuleList.Nodes.RemoveByKey("NodeSysSetting");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeASInOutGateJL");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodePAInOutGateJL");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeALLInOutGateJL");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeFczl");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeFCZLSX");
                //tvModuleList.Nodes["NodesMg"].Nodes.RemoveByKey("NodeFK");
                //tvModuleList.Nodes["NodesMg"].Nodes.RemoveByKey("NodeFKJL");
                //tvModuleList.Nodes["NodeSysSetting"].Nodes.RemoveByKey("NodeKQGL");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeDJKInOutGateJL");

            }
            else if (Session.CurrentUserName == "MKR")
            {
                tvModuleList.Nodes.RemoveByKey("NodeSysSetting");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeASInOutGateJL");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodePAInOutGateJL");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeALLInOutGateJL");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeFczl");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeFCZLSX");
                //tvModuleList.Nodes["NodesMg"].Nodes.RemoveByKey("NodeFK");
                //tvModuleList.Nodes["NodesMg"].Nodes.RemoveByKey("NodeFKJL");
                //tvModuleList.Nodes["NodeSysSetting"].Nodes.RemoveByKey("NodeKQGL");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeDJKInOutGateJL");

            }
           
            else if (Session.CurrentOrgType == 1)
            {
                tvModuleList.Nodes.RemoveByKey("NodeSysSetting");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodePAInOutGateJL");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeASInOutGateJL");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeALLInOutGateJL");
                tvModuleList.Nodes["NodesMg"].Nodes.RemoveByKey("NodeFK");
                tvModuleList.Nodes["NodesMg"].Nodes.RemoveByKey("NodeFKJL");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeFczlDJK");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeFCZLSXDJK");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeDJKInOutGateJL");
                //tvModuleList.Nodes.RemoveByKey("NodePAInOutGateJL");
                //tvModuleList.Nodes.RemoveByKey("NodeASInOutGateJL");
                //tvModuleList.Nodes.RemoveByKey("NodeALLInOutGateJL");
                //tvModuleList.Nodes["NodeSysSetting"].Nodes.RemoveByKey("NodeKQGL");

            }
            else if (Session.CurrentOrgType == 0)
            {
                tvModuleList.Nodes.RemoveByKey("NodeSysSetting");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodePAInOutGateJL");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeASInOutGateJL");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeALLInOutGateJL");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeFczlDJK");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeFCZLSXDJK");
                tvModuleList.Nodes["NodesZk"].Nodes.RemoveByKey("NodeDJKInOutGateJL");
              
                //tvModuleList.Nodes["NodeSysSetting"].Nodes.RemoveByKey("NodeKQGL");
                //if(Session.CurrentOrgCode != "01")
                //{
                //    tvModuleList.Nodes["NodeSysSetting"].Nodes.RemoveByKey("NodeKQGL");
                //}

            }
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void tvModuleList_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (bTreeNodeAutoSelect)
            {
                return;
            }
            TreeNode node = tvModuleList.SelectedNode;
            if (node != null && node.Level > 0)
            {
                string sClassName = node.Name.Replace("Node", "") + "Module";
                if (!FindTabPage(sClassName))
                {
                    IModule iModule = ModuleUtils.CreateModule(sClassName);
                    if (iModule != null)
                    {
                        CreateTabPage(iModule, sClassName, node.Text);
                    }
                }
            }
        }

        private bool FindTabPage(string key)
        {
            TabPage tp = tbcMain.TabPages[key];
            if (tp != null)
            {
                SelectTabPage(tp);
                return true;
            }
            else
            {
                return false;
            }
        }

        private void CreateTabPage(IModule iModule, string sKey, string sCaption)
        {
            TabPage tp = new TabPage() { Name = sKey, Text = sCaption };
            tp.HandleDestroyed += new EventHandler(TabPage_HandleDestroyed);
            tbcMain.TabPages.Add(tp);

            Control ctlModule = (Control)iModule;
            tp.Controls.Add(ctlModule);
            ctlModule.Parent = tp;
            ctlModule.Dock = DockStyle.Fill;
            SelectTabPage(tp);
        }

        private void TabPage_HandleDestroyed(object sender, EventArgs e)
        {
            if ((tbcMain.TabPages.Count == 0 || tbcMain.SelectedTab.Name == "tpDefault") && tvModuleList != null && tvModuleList.SelectedNode != null)
            {
                tvModuleList.SelectedNode = null;
            }
        }

        private void tbcMain_Selected(object sender, TabControlEventArgs e)
        {
            if (bTabPageAutoSelect)
            {
                return;
            }
            TabPage tp = tbcMain.SelectedTab;
            if (tp != null)
            {
                string sKey = "Node" + tp.Name.Replace("Module", "");
                TreeNode node = FindNode(tvModuleList.Nodes, sKey);
                if (node != null && node != tvModuleList.SelectedNode)
                {
                    SelectTreeNode(node);
                }
            }
        }

        private TreeNode FindNode(TreeNodeCollection collection, string sKey)
        {
            TreeNode resultNode;
            foreach (TreeNode node in collection)
            {
                if (node.Nodes != null && node.Nodes.Count > 0)
                {
                    resultNode = FindNode(node.Nodes, sKey);
                    if (resultNode != null)
                    {
                        return resultNode;
                    }
                }
                else if (node.Name == sKey)
                {
                    return node;
                }
            }
            return null;
        }

        private void SelectTabPage(TabPage tp)
        {
            bTabPageAutoSelect = true;
            try
            {
                tbcMain.SelectedTab = tp;
            }
            finally
            {
                bTabPageAutoSelect = false;
            }
        }

        private void SelectTreeNode(TreeNode node)
        {
            bTreeNodeAutoSelect = true;
            try
            {
                tvModuleList.SelectedNode = node;
            }
            finally
            {
                bTreeNodeAutoSelect = false;
            }
        }

        private void tsMenuItemExpandAll_Click(object sender, EventArgs e)
        {
            tvModuleList.ExpandAll();
        }

        private void tsMenuItemCollapseAll_Click(object sender, EventArgs e)
        {
            tvModuleList.CollapseAll();
        }

        private void tsMenuItemShow_Click(object sender, EventArgs e)
        {
            spcMain.Panel1Collapsed = false;
        }

        private void tsMenuItemHide_Click(object sender, EventArgs e)
        {
            spcMain.Panel1Collapsed = true;
        }

        private void tsMenuItemModifPwd_Click(object sender, EventArgs e)
        {
            using (ModifyPwdForm form = new ModifyPwdForm())
            {
                form.ShowDialog();
            }
        }

        private void tsMenuItemExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

       
       
    }
}
